#!/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect("www.xiezc.xyz", "root", "xzcbs1210", "daily", 8306)

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute()  方法执行 SQL 查询 

query_sql = "SELECT id,title,publish_time,`type` from xs_photo_album limit %s,50"

insert_sql = "insert into t_album(id,title,publish_time,`type`) values(%s,%s,%s,%s)"

try:
    start = 0
    data = []
    while True:
        cursor.execute(query_sql, start)
        print(start)
        start = start + 50
        for row in cursor:
            data.append(row)
        if len(data) == 0:
            break
        for row in data:
            cursor.execute(insert_sql, row)
        # 提交到数据库执行
        data.clear()
        db.commit()
except:
    # 如果发生错误则回滚
    db.rollback()
else:
    db.commit()

# 关闭数据库连接
db.close()
